<template>
	<div></div>
</template>

<script setup lang="ts">
import { useUserStore } from '@/stores/user'
import { ElLoading } from 'element-plus'
import apiMod from '@/apiMod/index'

const { userSSOLogin } = useUserStore()
const router = useRouter()
const route = useRoute()
const spinning = ref(false)
const login = () => {
	spinning.value = true
	const code = route.query?.code || 'YWRtaW4='
	const token = route.query?.token

	if (token) {
		apiMod
			.ssoTokenLogin(token)
			.then((res) => {
				if (res.data) {
          if(!res.data) return ElMessage.error('登录失败')
					const loading = ElLoading.service({
						lock: true,
						text: '正在进行登录授权',
						background: 'rgba(0, 0, 0, 0.7)',
					})
					const t_code = res.data.split('code=')[1]
					userSSOLogin(t_code, 'dWVpeEFhaHQ6dWVpeEFhaHQ=')
						.then((res) => {
							spinning.value = false
							router.push('/')
							loading.close()
						})
						.catch(() => {
							loading.close()
						})
				} else {
					ElMessage.error('登录失败')
				}
			})
			.catch((err) => {})
	} else {
		const loading = ElLoading.service({
			lock: true,
			text: '正在进行登录授权',
			background: 'rgba(0, 0, 0, 0.7)',
		})

		userSSOLogin(code,'c29jaWFsOnNvY2lhbA==')
			.then((res) => {
				spinning.value = false
				router.push('/')
				loading.close()
			})
			.catch(() => {
				loading.close()
			})
	}
}

onMounted(() => {
	login()
})
</script>

<style scoped></style>
